//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.payment;

import java.math.BigDecimal;
import org.jpgolay.me.buy.BuysPiece;
import org.jpgolay.me.customer.Customer;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.sale.SalesBillPiece;
import org.jpgolay.me.sale.SalesPiece;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.Parameter;

public class PaymentUpdatePiece {

    public PaymentUpdatePiece(Parameter param) {
        this.param = param;
        crud = new ObjectCRUD();
    }

    public void add(int pieceType, int updateType, BigDecimal cash, String pieceId) {
        this.pieceId = pieceId;
        this.cash = cash;
        switch (pieceType) {
            case 3: // '\003'
                addSalesBillPiece(updateType);
                break;

            case 4: // '\004'
                addSalesBillPiece(updateType);
                break;

            case 6: // '\006'
                addBuyPiece(updateType);
                break;

            case 0: // '\0'
                addSalesPiece();
                break;

            case 1: // '\001'
                addSalesPiece();
                break;

            case 2: // '\002'
                addSalesPiece();
                break;

            case 5: // '\005'
                addBuyPiece(updateType);
                break;
        }
    }

    public void remove(int pieceType, BigDecimal cash, String pieceId, BigDecimal piecePrice) {
        this.pieceId = pieceId;
        this.cash = cash;
        switch (pieceType) {
            case 3: // '\003'
                removeSalesBillPiece(piecePrice);
                break;

            case 4: // '\004'
                removeSalesBillPiece(piecePrice);
                break;

            case 6: // '\006'
                removeBuyPiece();
                break;

            case 0: // '\0'
                removeSalesPiece();
                break;

            case 1: // '\001'
                removeSalesPiece();
                break;

            case 2: // '\002'
                removeSalesPiece();
                break;

            case 5: // '\005'
                removeBuyPiece();
                break;
        }
    }

    private void addSalesPiece() {
        SalesPiece piece = (SalesPiece) crud.read(pieceId, 22);
        if (piece != null) {
            piece.setUpdateUserId(param.getUserId());
            piece.setPartPayment(cash);
            crud.update(piece, 22);
        }
    }

    private void addSalesBillPiece(int type) {
        MEDate d = new MEDate();
        SalesBillPiece piece = (SalesBillPiece) crud.read(pieceId, 25);
        if (piece != null) {
            piece.setUpdateUserId(param.getUserId());
            if (type == 0) {
                piece.setClearDate(d.getPlain());
            }
            piece.setPartPayment(cash);
            crud.update(piece, 25);
            if (type == 0) {
                Customer customer = (Customer) crud.read(piece.getCustomerId(), 27);
                if (customer != null) {
                    customer.setUserId(param.getUserId());
                    if (piece.getPrefix().equals("F")) {
                        customer.setTotalEncaisser(customer.getTotalEncaisser().add(cash));
                    } else if (piece.getPrefix().equals("A")) {
                        customer.setTotalEncaisser(customer.getTotalEncaisser().subtract(cash));
                    }
                    crud.update(customer, 27);
                }
            }
        }
    }

    private void addBuyPiece(int type) {
        MEDate d = new MEDate();
        BuysPiece piece = (BuysPiece) crud.read(pieceId, 24);
        if (piece != null) {
            piece.setUpdateUserId(param.getUserId());
            if (type == 0) {
                piece.setClearDate(d.getPlain());
            }
            piece.setPartPayment(cash);
            crud.update(piece, 24);
        }
    }

    private void removeSalesPiece() {
        SalesPiece piece = (SalesPiece) crud.read(pieceId, 22);
        if (piece != null) {
            piece.setUpdateUserId(param.getUserId());
            piece.setPartPayment(piece.getPartPayment().subtract(cash));
            crud.update(piece, 22);
        }
    }

    private void removeSalesBillPiece(BigDecimal piecePrice) {
        MEDate d = new MEDate();
        SalesBillPiece piece = (SalesBillPiece) crud.read(pieceId, 25);
        if (piece != null) {
            piece.setUpdateUserId(param.getUserId());
            boolean updateCustomer = false;
            if (!piece.getClearDate().equals("")) {
                piece.setClearDate("");
                updateCustomer = true;
            }
            piece.setPartPayment(piece.getPartPayment().subtract(cash));
            crud.update(piece, 25);
            if (updateCustomer) {
                Customer customer = (Customer) crud.read(piece.getCustomerId(), 27);
                if (customer != null) {
                    customer.setUserId(param.getUserId());
                    if (piece.getPrefix().equals("F")) {
                        customer.setTotalEncaisser(customer.getTotalEncaisser().subtract(piecePrice));
                    } else if (piece.getPrefix().equals("A")) {
                        customer.setTotalEncaisser(customer.getTotalEncaisser().add(piecePrice));
                    }
                    crud.update(customer, 27);
                }
            }
        }
    }

    private void removeBuyPiece() {
        MEDate d = new MEDate();
        BuysPiece piece = (BuysPiece) crud.read(pieceId, 24);
        if (piece != null) {
            piece.setUpdateUserId(param.getUserId());
            if (!piece.getClearDate().equals("")) {
                piece.setClearDate("");
            }
            piece.setPartPayment(piece.getPartPayment().subtract(cash));
            crud.update(piece, 24);
        }
    }
    private String pieceId;
    private BigDecimal cash;
    private Parameter param;
    private ObjectCRUD crud;
}
